// pages/myOrders/myOrders.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

    myOrders: [], //用户所有订单信息汇总
    status: [],
    swiperId: 0 //swiper当前所选择id     
  },

  //*******************************************  上方的状态栏【点击事件】
  changeStatusClick: function (e) {
    let id = e.currentTarget.dataset.id;
    this.setData({
      swiperId: id
    })
  },
  //*******************************************  swiper【滑动事件】
  swiperChangeId: function (e) {
    let id = e.detail.current;
    this.setData({
      swiperId: id
    })
  },
  //*******************************************  获取当前用户的历史订单
  getMyOrders: function () {
    let that = this;
    let userId = wx.getStorageSync('userInfo').userid;
    if (userId) {
      //userId已经定义，说明当前用户已经登录
    }
    else {
      wx.navigateBack();
      //说明当前没有登录成功
      wx.showModal({
        title: '友情提示',
        content: '获取订单失败，请检查是否登陆成功',
        showCancel: false
      })
      return;
    }
    wx.showLoading({
      title: '正在加载'
    })
    //----------------------------------------------调用服务端API获取订单信息
    wx.cloud.callFunction({
      name: 'myOrders',
      data: {
        userId: userId
      }
    }).then(res => {
      let data = res.result;
      console.log(data)
      wx.hideLoading()
      that.setData({
        myOrders: data.orders,
        status: data.status
      })
    }).catch(err => {
      wx.hideLoading()
      wx.showModal({
        title: '友情提示',
        content: '获取订单失败，请检查网络状态',
        showCancel: false
      })
    })
  },
  //*******************************************  用户点击某单后，查看订单详情
  checkOrderDetail:function (e) {
    let that = this;
    let orderId = e.currentTarget.dataset.id;
    //console.log(orderId)
    wx.showLoading({ title: '正在查询' })
    const db=wx.cloud.database();
    db.collection('orders').where({ orderId: orderId }).get()
    .then(res=>{
          getApp().globalData.orderDetail = res.data[0];
          wx.navigateTo({
            url: '../../pages/orderDetail/orderDetail'
          })
          wx.hideLoading();
    })
    .catch(err=>{
          wx.hideLoading();
          wx.showModal({
            title: '友情提示',
            content: '读取订单信息出错:' + err,
            showCancel: false
          })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMyOrders();
  },
  /**
  * 生命周期函数--下拉刷新
  */
  onPullDownRefresh: function () {
    this.getMyOrders();
    wx.stopPullDownRefresh();
  }
})